﻿using AiMoBridge.Interface;
using AiMoWindows.Windows.AiAssistant.ViewModels;
using System.Windows;
using System.Windows.Controls;

namespace AiMoWindows.Windows.AiAssistant
{
    /// <summary>
    /// AiAssistantWindow.xaml 的交互逻辑
    /// </summary>
    partial class AiAssistantWindow : Window, IAiMoWindow
    {
        readonly AiAssistantViewModel m_ViewModel;
        public AiAssistantWindow()
        {
            m_ViewModel = new AiAssistantViewModel(this);
            DataContext = m_ViewModel;
            InitializeComponent();
            AddEvent();
        }
        public void Init()
        {
            m_ViewModel.Init();
        }
        void AddEvent()
        {
            ReplyTextbox.PreviewMouseWheel += m_ViewModel.AiAssistantUI.MouseWheelChangeSize;
            OutputTextBox.PreviewMouseWheel += m_ViewModel.AiAssistantUI.MouseWheelChangeSize;
        }
        void RemoveEvent()
        {
            ReplyTextbox.PreviewMouseWheel -= m_ViewModel.AiAssistantUI.MouseWheelChangeSize;
            OutputTextBox.PreviewMouseWheel -= m_ViewModel.AiAssistantUI.MouseWheelChangeSize;
        }

        bool _ClosedActived = false;
        bool IAiMoWindow.CloseActived => _ClosedActived;
        void IAiMoWindow.UpdateState()
        {
            m_ViewModel.AiAssistantUI.UpdateAllUIWindows();
            m_ViewModel.AiAssistantUI.UpdateAttention();
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            RemoveEvent();
            _ClosedActived = true;
        }
        void IDebugPrinter.PrintDebug(string text)
        {
            PrintDebug(text);
        }
        public void PrintDebug(string message)
        {
            if (!_ClosedActived && !string.IsNullOrEmpty(message))
            {
                OutputTextBox.Dispatcher.InvokeAsync(() =>
                {
                    OutputTextBox.AppendText($"[{DateTime.Now:HH:mm:ss}] " + message + Environment.NewLine);
                    OutputTextBox.ScrollToEnd();
                });
            }
        }
        private void ReplyTextbox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            TextBox box = (TextBox)sender;
            box.ScrollToEnd();
        }
    }
}
